import pandas as pd
import os
from collections import defaultdict

test = pd.read_table('../../data/orders_test_spu.txt', sep='\t')

ffm_file = '../solution_2/submit/submit-ffm_50_spu_ffm_1.txt'
catboost_file = '../solution_3/submit/submit-catboost_50_spu_5.txt'

user_KGIN_RI_file = '../solution_1/result/3_top_50_submit_KGIN_data_KGIN_RI_test.txt' # 注意：这个文件与前面两个不同，这个是基于用户。
KGIN_RI_file = '../solution_1/submit/3_KGIN_RI_submit_KGIN_data_50.txt'

if os.path.exists(KGIN_RI_file):
    pass
else:
    # 将KGIN_RI的结果转换为基于订单的
    user_bprmf_recall_spu = defaultdict(list)
    for line in open(user_KGIN_RI_file):
        lines = line.strip().split('\t')
        user_bprmf_recall_spu[int(lines[0])].extend([int(a) for a in lines[1:]])
    with open(KGIN_RI_file, 'w') as f:
        for order, user in zip(test['wm_order_id'], test['user_id']):
            f.write(str(order))
            for spu in user_bprmf_recall_spu[user]:
                f.write('\t')
                f.write(str(spu))
            f.write('\n')


order_rank = {}
f1 = open('./submit/submit-mean_score_50spu_15.txt', 'w')  # 存放每个订单推荐10个的结果，多出的5个作为备用，因为后面会有很多过滤处理。
with open('./submit/submit-mean_score_15.txt', 'w') as f:
    for ffm_line, catboost_line, kgin_ri_line in zip(open(ffm_file), open(catboost_file), open(KGIN_RI_file)):
        ffm_dict = {}
        catboost_dict = {}
        kgin_ri_dict = {}
        ensemble_dict = {}

        rank_index = 0
        ffm_line = [int(a) for a in ffm_line.strip('\n').split('\t')]
        for spu in ffm_line[1:]:
            ffm_dict[spu] = rank_index / 49  # 对排名进行标准化，为后面求取每个spu的平均值，然后重新排序做准备
            rank_index += 1

        rank_index = 0
        catboost_line = [int(a) for a in catboost_line.strip('\n').split('\t')]
        for spu in catboost_line[1:]:
            catboost_dict[spu] = rank_index / 49  # 对排名进行标准化，为后面求取每个spu的平均值，然后重新排序做准备
            rank_index += 1

        rank_index = 0
        kgin_ri_line = [int(a) for a in kgin_ri_line.strip('\n').split('\t')]
        for spu in kgin_ri_line[1:]:
            kgin_ri_dict[spu] = rank_index / 49  # 对排名进行标准化，为后面求取每个spu的平均值，然后重新排序做准备
            rank_index += 1

        for spu in ffm_dict:
            ensemble_dict[spu] = 0.9*ffm_dict[spu] + catboost_dict[spu] + 0.9*kgin_ri_dict[spu]  # 只融合后几个

        S = sorted(ensemble_dict.items(), key=lambda item: item[1], reverse=False)
        order_rank[ffm_line[0]] = S
        f.write(str(ffm_line[0]))
        count = 0
        for s in S:
            f.write('\t')
            f.write(str(s[0]))
            count += 1
            if count == 5:
                break
        f.write('\n')

        f1.write(str(ffm_line[0]))
        count = 0
        for s in S:
            f1.write('\t')
            f1.write(str(s[0]))
            count += 1
            if count == 50:
                break
        f1.write('\n')